package com.chenfan.process.context;

import com.chenfan.notify.dto.NotifyDTO;
import com.chenfan.process.event.callback.NotifyEvent;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author K
 * @date 2022/7/12 5:12 PM
 */
public class NotifyEventContext {
    private static final InheritableThreadLocal<NotifyEvent> EVENT_CONTEXT = new InheritableThreadLocal<>();

    private NotifyEventContext() {}

    public static void addEvent(List<NotifyDTO> notifyList) {
        NotifyEvent event = EVENT_CONTEXT.get();
        if (Objects.isNull(event)) {
            event = new NotifyEvent(new ArrayList<>());
        }
        List<NotifyDTO> callbackEvents = event.getNotifyList();
        callbackEvents.addAll(notifyList);
        EVENT_CONTEXT.set(event);
    }

    public static NotifyEvent getEvent() {
        return EVENT_CONTEXT.get();
    }


    public static void clear() {
        EVENT_CONTEXT.remove();
    }
}
